package com.mmall.controller.portal;

import com.mmall.common.Const;
import com.mmall.common.ResponseCode;
import com.mmall.common.ServerResponse;
import com.mmall.pojo.User;
import com.mmall.service.IUserService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * Created by yanglin on 2019/3/3.
 */
@Controller
@RequestMapping("/user/")
public class UserController {


    @Autowired
    private IUserService iUserService;
/*
  用户登录
   username
   password
   session
 */
    @RequestMapping(value="login.do",method= RequestMethod.POST)
    @ResponseBody
    public ServerResponse<User> login(String username, String password, HttpSession session){


        ServerResponse<User> response = iUserService.login(username,password);
        if(response.isSucess()){
           session.setAttribute(Const.CURRENT_USER,response.getData());  //将用户信息放到这个 session中
        }
        return response;
    }

    @RequestMapping(value="logout.do",method= RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> logout(HttpSession session){
            session.removeAttribute(Const.CURRENT_USER);
        return ServerResponse.createBySuccess();
    }

    @RequestMapping(value="register.do",method= RequestMethod.POST)   //请求地址
    @ResponseBody
    public ServerResponse<String> register(User user){
        //springMVC 数据绑定  类转化器
        return iUserService.register(user);
    }

    @RequestMapping(value="check_Valid.do",method= RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> checkValid(String str,String type){
      return iUserService.checkValid(str,type);
    }

    @RequestMapping(value=" getUserInfo.do",method= RequestMethod.POST)
    @ResponseBody
    public ServerResponse<User> getUserInfo(HttpSession session){
        User user =(User) session.getAttribute(Const.CURRENT_USER);
        if(user!=null){
            return ServerResponse.createBySuccess(user);
        }
        return ServerResponse.createByErrorMessage("用户未登陆，无法获取用的信息");
    }

    //密码提示问题的获取  忘记密码
    @RequestMapping(value="forget_Get_Question.do",method= RequestMethod.POST)   //请求地址
    @ResponseBody
    public ServerResponse<String> forgetGetQuestion(String username){

            return iUserService.selectQuestion(username);
    }

    //使用本地缓存检查问题答案的接口
    @RequestMapping(value="forget_Check_Answer.do",method= RequestMethod.POST)   //请求地址
    @ResponseBody
    public ServerResponse<String> forgetCheckAnswer(String username,String question,String answer){
         return iUserService.checkAnswer(username,question,answer);
    }



    @RequestMapping(value="forget_Reset_Password.do",method= RequestMethod.POST)   //请求地址
    @ResponseBody
    public ServerResponse<String> forgetResetPassword(String username,String passwordNew,String forgetToken){
         return iUserService.forgetResetPassword(username,passwordNew,forgetToken);
    }

    //登录状态的重置密码
    @RequestMapping(value="reset_Password.do",method= RequestMethod.POST)   //请求地址
    @ResponseBody
    public ServerResponse<String> resetPassword(HttpSession session,String passwordOld,String passwordNew){
        User user=(User)session.getAttribute(Const.CURRENT_USER);
        if(user==null){
            return ServerResponse.createByErrorMessage("用户未登录");
        }
            return iUserService.resetPassword(passwordOld,passwordNew,user);

    }

    @RequestMapping(value="update_information.do",method= RequestMethod.POST)   //请求地址
    @ResponseBody
   public ServerResponse<User> update_information(HttpSession session,User user){
        User currentUser = (User)session.getAttribute(Const.CURRENT_USER);
       if(currentUser==null){
           return ServerResponse.createByErrorMessage("用户未登陆");
       }
        user.setId(currentUser.getId());
        user.setUsername(currentUser.getUsername());
   //    return iUserService.update_information(user);
        ServerResponse<User> response = iUserService.update_information(user);
        if(response.isSucess()){
            session.setAttribute(Const.CURRENT_USER,response.getData());
        }
        return response;
   }

    @RequestMapping(value="get_information.do",method= RequestMethod.POST)   //请求地址
    @ResponseBody
    public ServerResponse<User> get_information(HttpSession session)
    {
        User currentUser=(User) session.getAttribute(Const.CURRENT_USER);
        if(currentUser==null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登录");
        }
        return iUserService.getInformation(currentUser.getId());
    }

}
